package nc.pub.cgdd.utils.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import nc.bs.dao.BaseDAO;
import nc.bs.dao.DAOException;
import nc.bs.framework.common.NCLocator;
import nc.itf.uap.IUAPQueryBS;
import nc.jdbc.framework.processor.ColumnProcessor;
import nc.jdbc.framework.processor.MapListProcessor;
import nc.vo.pub.BusinessException;
import nc.vo.pub.SuperVO;

/**
 * JdbcUtil：JDBC工具类
 * 
 * @author CYQ
 * @date 2021年11月25日 上午11:22:47
 * @version 1.0.0
 */
public class JdbcUtil {

	private static JdbcUtil jdbcutil = new JdbcUtil();
	private static BaseDAO baseDAO = null;
	private static IUAPQueryBS iuap = (IUAPQueryBS) NCLocator.getInstance().lookup(IUAPQueryBS.class.getName());

	private JdbcUtil() {
	}

	public static String insertVO(SuperVO vo) throws DAOException, BusinessException {
		return getBaseDAO().insertVO(vo);
	}

	public static BaseDAO getBaseDAO() throws BusinessException {
		if (baseDAO == null) {
			baseDAO = new BaseDAO();
		}
		return baseDAO;
	}

	// 通过该方法获得实例对象
	public static JdbcUtil getJdbcutil() {
		return jdbcutil;
	}

	/**
	 * 单个查询条件数据库查询操作
	 */
	public static String queryColumn(String tablename, String showname, String key, String value)
			throws BusinessException {
		String sql = "select " + showname + " from " + tablename + "  where isnull(dr,0) = 0 and " + key + " = '"
				+ value + "'";
		Object result = iuap.executeQuery(sql, new ColumnProcessor());

		return result == null ? "" : result.toString();
	}

	/**
	 * 单个查询条件是否存在数据库操作
	 */
	public static boolean whetheExistBySelectSql(String tableName, String columnName, String columnValue)
			throws BusinessException {
		String sql = "select count(*) from " + tableName + "  where isnull(dr,0) = 0 and " + columnName + " = '"
				+ columnValue + "'";
		int count = Integer.parseInt(iuap.executeQuery(sql, new ColumnProcessor()).toString());

		boolean isExist = false;
		if (count == 0) {
			return isExist;
		} else {
			isExist = true;
		}

		return isExist;
	}

	public static Object queryColumnBySelectSql(String tableName, String selectColumn, String columnName,
			String columnValue) throws BusinessException {
		String sql = "select " + selectColumn + " from " + tableName + "  where isnull(dr,0) = 0 and " + columnName
				+ " = '" + columnValue + "'";
		Object result = iuap.executeQuery(sql, new ColumnProcessor());

		return result == null ? "" : result.toString();
	}

	/**
	 * 单个查询条件数据库查询操作
	 */
	public static String queryColumn(String sql) throws BusinessException {
		Object obj = iuap.executeQuery(sql.toString(), new ColumnProcessor());
		return obj == null ? "" : obj.toString();
	}

	/**
	 * queryDefdocToString：自定义档案查询工具 创 建 人 ：CYQ 创建时间：2021年11月26日-上午10:28:49
	 */
	public static String queryDefdocToString(String tablename, String showname, String key, String value,
			String listcode) throws BusinessException {
		String sql = "select " + showname + " from " + tablename
				+ "  left join bd_defdoclist  on bd_defdoc.pk_defdoclist=bd_defdoclist.pk_defdoclist  "
				+ " where isnull(bd_defdoc.dr,0) = 0 and " + key + " = '" + value + "' and bd_defdoclist.code='"
				+ listcode + "' ";
		Object result = iuap.executeQuery(sql, new ColumnProcessor());
		if (result == null) {
			result = "";
		}
		return result.toString();
	}

	/**
	 * queryDefdoc： 自定义档案翻译工具 创 建 人 ：CYQ 创建时间：2021-4-16-下午1:59:36
	 */
	public static String queryDefdoc(String listcode, String showname, String key, String value)
			throws BusinessException {
		StringBuffer sql = new StringBuffer();
		sql.append("	select " + showname + "	");
		sql.append("	  from bd_defdoc	");
		sql.append("	 where pk_defdoclist in	");
		sql.append("	       (select pk_defdoclist from bd_defdoclist where (code = '" + listcode + "' or name = '"
				+ listcode + "'))	");
		sql.append("	   and isnull(dr, 0) = 0	");
		sql.append("	   and " + key + " = '" + value + "'	");

		Object result = iuap.executeQuery(sql.toString(), new ColumnProcessor());
		if (result == null) {
			result = "";
		}
		return result.toString();
	}

	/**
	 * queryDBMap： 查询Map结果集 创 建 人 ：CYQ 创建时间：2021-1-15-下午4:24:36
	 */
	public static List<Map<String, Object>> queryDBMap(String sql) throws BusinessException {

		List<Map<String, Object>> listmap = (List<Map<String, Object>>) iuap.executeQuery(sql.toString(),
				new MapListProcessor());
		if (listmap != null && listmap.size() != 0) {
			return listmap;
		}
		return new ArrayList<Map<String, Object>>();
	}

	/**
	 * 该方法是获取功能注册相应参数的Value值
	 * 
	 * @param initcode
	 * @return
	 */
	public static String getInitValue(String initcode, String pk_org) {
		IUAPQueryBS lookup = NCLocator.getInstance().lookup(IUAPQueryBS.class);
		Object sysInitVOs = null;
		try {
			String sql = "select value from pub_sysinit where  pub_sysinit.initcode='" + initcode
					+ "' and pub_sysinit.pk_org='" + pk_org + "' and isnull(dr,0)=0";
			sysInitVOs = lookup.executeQuery(sql, new ColumnProcessor());
			return sysInitVOs == null ? "" : sysInitVOs.toString();
		} catch (BusinessException e) {
			return "";
		}
	}

	/**
	 * 该方法是获取功能注册相应全局参数的Value值
	 * 
	 * @author 曹原
	 * @since 2021年12月16日 15:33
	 * @param initcode
	 * @return
	 */
	public static String getInitValueGlobal(String initcode) {
		IUAPQueryBS lookup = NCLocator.getInstance().lookup(IUAPQueryBS.class);
		Object sysInitVOs = null;
		try {
			String sql = "select value from pub_sysinit where  pub_sysinit.initcode='" + initcode
					+ "' and isnull(dr,0)=0";
			sysInitVOs = lookup.executeQuery(sql, new ColumnProcessor());
			return sysInitVOs == null ? "" : sysInitVOs.toString();
		} catch (BusinessException e) {
			return "";
		}
	}
	public static int update(String sql) throws DAOException, BusinessException {
		int executeUpdate = getBaseDAO().executeUpdate(sql);
		return executeUpdate;
		
	}

	
}
